package com.bwsoft.admin.action;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import net.sf.json.JSONObject;

import com.bwsoft.admin.domain.Banner;
import com.bwsoft.admin.domain.valueObject.BannerVO;

public class BannerAction extends BaseAction_ybd{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1713055194863128376L;

	String s_bannerName;
	String s_status;
	
	public String bannerIds;
	
	public String bannerIndex(){
		return "bannerIndex";
	}
	
	/**
	 * 查询获取列表
	 */
	public String getBannerJson() {

		List<Banner> bannerSet=null;
			//通过字典编号查询字典详情的分页集合
		bannerSet = bannerService.findData(page, rows, s_bannerName,  s_status);
			//通过编号查询字典集合的大小	
		Long count = bannerService.getTotalCount(s_bannerName,  s_status);
		
		List<BannerVO> bannerVos = bannerService.getVo(bannerSet);
		
		responseJson.put("total", count);
		responseJson.put("rows", bannerVos);

		return "mapJsonResult";
	}

	/**
	 * 保存或新增
	 * @return
	 */
	public String saveOrUpdateBanner(){
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		
		if(banner.getBannerOrder() < 0 || banner.getBannerOrder() > 100){
//			responseJson.put("result", "Fail");
//			responseJson.put("message", "超出范围！请把排序号设定在'0-100'之间");
//			return "mapJsonResult";
			json.put("result", "Fail");
			json.put("message", "超出范围！请把排序号设定在'0-100'之间");
			try {
				PrintWriter out = response.getWriter();
				out.write(json.toString());
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return null;
		}
		
		if(banner.getBannerId() == null || "".equals(banner.getBannerId())){
			if(bannerImg == null || "".equals(bannerImg)){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "请上传Banner图！");
//				return "mapJsonResult";
				
				json.put("result", "Fail");
				json.put("message", "请上传Banner图！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return null;
			}
			if(bannerImg != null && !"".equals(bannerImg) && !newsService.isImage(bannerImgContentType)){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "Banner图上传的文件不是图片类型！");
//				return "mapJsonResult";
				
				json.put("result", "Fail");
				json.put("message", "Banner图上传的文件不是图片类型！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return null;
			}
			if(bannerImg != null && !"".equals(bannerImg) && !newsService.isFitSize(bannerImg)){
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "Banner图上传的图片不能超过8M！");
//				return "mapJsonResult";
				
				json.put("result", "Fail");
				json.put("message", "Banner图上传的图片不能超过8M！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return null;
			}
			BufferedImage bi;
			try {
				bi = ImageIO.read(bannerImg);
				int width = bi.getWidth();
				int height = bi.getHeight();
//				if(width > 1920 || height > 738){
//					responseJson.put("result", "Fail");
//					responseJson.put("message", "Banner图上传的图片不能超过1920*738！");
//					return "mapJsonResult";
//				}
				banner.setBannerSize(width+"*"+height);
			} catch (IOException e) {
				e.printStackTrace();
//				responseJson.put("result", "Fail");
//				responseJson.put("message", "Banner图上传失败！");
//				return "mapJsonResult";
				
				json.put("result", "Fail");
				json.put("message", "Banner图上传失败！");
				try {
					PrintWriter out = response.getWriter();
					out.write(json.toString());
					out.flush();
					out.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
				
				return null;
			}
			
		}
		msg = bannerService.saveOrUpdateBanner(banner, bannerImg, bannerImgFileName);
		
//		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
//		responseJson.put("message", msg.getMsgStr());
//		return "mapJsonResult";
		
		json.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		json.put("message", msg.getMsgStr());
		try {
			PrintWriter out = response.getWriter();
			out.write(json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	/**
	 * 获取详情内容
	 * @return
	 */
	public String bannerModifyInfo(){
		banner = bannerService.bannerModifyInfo(banner.getBannerId());
		
		List<Banner> bannerList = new ArrayList<Banner>();
		bannerList.add(banner);
		List<BannerVO> listVo = bannerService.getVo(bannerList);
		
		responseJson.put("banner", listVo.get(0));
		return "mapJsonResult";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String remove(){
		
		msg = bannerService.remove(bannerIds);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	/**
	 * 状态改为显示
	 * @return
	 */
	public String show(){
		response.setContentType("text/html;charset=utf-8");
		JSONObject json = new JSONObject();
		
		String[] aId = bannerIds.split(",");
		if(aId.length > 5){
			responseJson.put("result", "Fail");
			responseJson.put("message", "Banner图最多显示5个！");
			return "mapJsonResult";
		}
		
		msg = bannerService.show(bannerIds);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	/**
	 * 状态改为隐藏
	 * @return
	 */
	public String hide(){
		
		msg = bannerService.hide(bannerIds);
		
		responseJson.put("result", msg.getMsgFlag() ? "Success" : "Fail");
		responseJson.put("message", msg.getMsgStr());
		return "mapJsonResult";
	}
	
	
//	-------------------------------------------------------
	public String getS_bannerName() {
		return s_bannerName;
	}

	public void setS_bannerName(String s_bannerName) {
		this.s_bannerName = s_bannerName;
	}

	public String getS_status() {
		return s_status;
	}

	public void setS_status(String s_status) {
		this.s_status = s_status;
	}

	public String getBannerIds() {
		return bannerIds;
	}

	public void setBannerIds(String bannerIds) {
		this.bannerIds = bannerIds;
	}
	
	
}
